package cn.com.gridinfo.sooc.core.my.personal.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;


/**
 * 修改昵称
 * <p/>
 * Created by 刘晓晨 on 2015/12/16.
 */
public class ChangeNicknameActivity extends BaseSoocActivity {

    @InjectView(R.id.nickName)
    ClearEditText nickName;//新昵称输入框
    @InjectView(R.id.btn_send)
    Button btnSend;// 提交按钮
    private String nick;//定义全局，用来拿上个界面传递来的昵称
    MyDao dao;
    private String newNick;//新的昵称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_change_nickname);
        ButterKnife.inject(this);
        dao = new MyDao(this);
        nick = getIntent().getStringExtra("nickname");//拿到传来的昵称 显示
        nickName.setText(nick);
        nickName.setSelection(nickName.length());
        nickName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    if (validateNotNull()) {
                        change();
                    }
                }
                return false;
            }
        });

//        btnSend.setEnabled(false);//设置按钮不能点击时候的状态
    }

    //监听输入框状态，
    @OnTextChanged(value = R.id.nickName, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {

        if (StringUtils.isNull(nickName.getText().toString().trim())) {
            btnSend.setEnabled(false);
        } else {
            btnSend.setEnabled(true);
        }
    }

    private boolean validateNotNull() {
        //账号为空
        if (StringUtils.isNull(nickName.getText().toString().trim())) {
            nickName.requestFocus();
            return false;
        }
        return true;
    }


    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "昵称";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();
    }

    //点击更换昵称
    @OnClick(R.id.btn_send)
    void change() {
        newNick = nickName.getText().toString().trim();
        if (newNick.length() < 1 || newNick.length() > 10) {
            MessageUtils.showImageShortToast(this, "请输入1~10位昵称");
        } else {
            dao.updateInfo(newNick, null, null, -1);
        }
    }

    //请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.status.equals("0")) {
                setResult(RESULT_OK);
                MessageUtils.showRightImageShortToast(this, dao.statusCode);
                Arad.preferences.putString(LocalMark.USER_NICKNAME.getName().toString(), newNick);//将新的昵称存本地
                Arad.preferences.flush();
                finish();
            } else {
                MessageUtils.showImageShortToast(this, dao.statusCode);
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "数据请求异常");
        }
    }
}
